# Methods added to this helper will be available to all templates in the application.


module ApplicationHelper 
  def in_place_collection_editor_field(object, method, container, tag_options={}, options={})
      tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
      tag_options = { :tag => "span",
        :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor",
        :class => "in_place_editor_field" }.merge!(tag_options)
      url = url_for(options[:url]) || url_for( :action => "set_#{object}_#{method}", :id => tag.object.id )
      collection = container.inject([]) do |options, element|
        options << "[ '#{escape_javascript(element.last.to_s)}', '#{escape_javascript(element.first.to_s)}']" 
      end
      function =  "new Ajax.InPlaceCollectionEditor("
      function << "'#{object}_#{method}_#{tag.object.id}_in_place_editor',"
      function << "'#{url}',"
      function << "{collection: [#{collection.join(',')}], id: '#{object}_#{method}'});"
      tag.to_content_tag(tag_options.delete(:tag), tag_options) + javascript_tag(function)
  end
  
end

class Mock
  attr_writer :me
  def initialize(a_me)
    @me = a_me
  end
  def id
    @me.id
  end
  def method_missing(symbol, *args)
    puts "Called #{symbol} with #{args}"
    @me.send(symbol, *args)
  end
end